import {toFixedVector} from "../util/StaticMethods.js"
import * as THREE from "../../../modules/three/three.module.js"

let modelCount = 1;
export function setModelCount(num) {
  if (modelCount <= num) {
    modelCount = num + 1;
  }
}

export default class ModelVO {
  id = 0
  url = ''

  position = new THREE.Vector3();
  rotation = new THREE.Euler();
  scale = new THREE.Vector3(1, 1, 1);
  children = []
  visible = true
  locked = false

  constructor(url) {
    if (url) {
      this.id = modelCount++;
      this.url = url;
    }
  }

  getUrl() {
    return this.url;
  }

  get isLand() {
    return this.url.indexOf('dx_') >= 0;
  }

  supportLayers() {
    return this.url && (this.url.endsWith('BanGongLou2.glb') || this.url.endsWith('HQZX.glb'))
  }

  // true 表示属于室内模型
  isIndoorModel() {
    return this.url && (this.url.endsWith('F2_detail.glb') || this.url.endsWith('BanGongShi.glb'))
  }

  // 返回true 该模型是烘焙阴影
  isShadow() {
    return this.url.indexOf('/baked_shadow/') > 0
  }

  isSelectable() {
    return this.visible && !this.locked;
  }

  toJsonObj() {
    const {id, url, visible, locked} = this;
    const obj = {id, url, visible, locked};
    obj.position = toFixedVector(this.position, 3).toArray();
    obj.rotation = toFixedVector(this.rotation, 3).toArray();
    obj.scale = toFixedVector(this.scale, 3).toArray();
    return obj;
  }

  fromJsonObj(jsonObj) {
    this.id = jsonObj.id;
    setModelCount(this.id);
    this.url = jsonObj.url;
    this.position.fromArray(jsonObj.position);
    this.rotation.fromArray(jsonObj.rotation);
    this.scale.fromArray(jsonObj.scale);
    if (jsonObj.children && jsonObj.children.length) {
      this.children = jsonObj.children;
    }
    if (jsonObj.visible !== undefined) {
      this.visible = jsonObj.visible;
    }
    this.locked = jsonObj.locked;
    return this;
  }
}
